package org.lyh.base.service.impl;

import org.lyh.base.service.IVerifyCodeservice;
import org.lyh.base.util.SendMsgWebchineseUtil;
import org.lyh.base.util.StrUtils;
import org.lyh.base.util.VerifyCodeUtils;
import org.lyh.user.domain.Logininfo;
import org.lyh.user.domain.User;
import org.lyh.user.mapper.LogininfoMapper;
import org.lyh.user.mapper.UserMapper;
import org.lyh.user.pojo.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeserviceImpl implements IVerifyCodeservice {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public String getImageCode(String imageCodeKey) throws IOException {
        //通过工具类生成验证码
        String complexRandomString = StrUtils.getComplexRandomString(6);
        //存redis
        redisTemplate.opsForValue().set(imageCodeKey,complexRandomString);
        //设置过期时间为5分钟
        redisTemplate.expire(imageCodeKey,5, TimeUnit.MINUTES);
        //生成并返回base64内容
        return VerifyCodeUtils.VerifyCode(120,30,complexRandomString);
    }
    @Override
    public void sendSmsCode(UserDTO userDTO) {
        String msg="您的验证码为：";//短信模版
        //判断空值
        if (Objects.isNull(userDTO.getMobile())
                || Objects.isNull(userDTO.getImageCode())
                || Objects.isNull(userDTO.getImageCodeKey())) {
            throw new RuntimeException("参数不能为空！");
        }
        //图形码是否正确
        if (!userDTO.getImageCode().equals(redisTemplate.opsForValue().get(userDTO.getImageCodeKey()))) {
            throw new RuntimeException("图形码不正确！");
        }
        //手机号是否已经注册
        if (Objects.nonNull(logininfoMapper.queryByPhone(userDTO.getMobile()))) {
            throw new RuntimeException("手机号已经注册！");
        }
        //生成手机验证码
        String randomString = StrUtils.getRandomString(6);
        //判断redis是否已经保存了验证码，没有就生成新的验证码发送
        Map<String,Object> entries = redisTemplate.opsForHash().entries("reg_"+userDTO.getMobile());
        if (entries.isEmpty()) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("smsCode",randomString);
            map1.put("smsCodeTime",new Date().getTime());
            //存redis
            redisTemplate.opsForHash().putAll("reg_"+userDTO.getMobile(),map1);
            //发送短信
            String result = SendMsgWebchineseUtil.doSendMsg(userDTO.getMobile(),msg+randomString);
        }else {
            //判断是否在60s内
            Long smsCodeTime = (Long)entries.get("smsCodeTime");
            if ((new Date().getTime() - smsCodeTime) <= 60000) {
                throw new RuntimeException("60s内不能重复发送！");
            }
            //获取旧的验证码
            String smsCode = (String) entries.get("smsCode");
            //发送旧的验证码
            String result = SendMsgWebchineseUtil.doSendMsg(userDTO.getMobile(),msg+randomString);
            //更新redis过期时间为5分钟
            redisTemplate.expire("reg_"+userDTO.getMobile(),5, TimeUnit.MINUTES);
        }
    }
    //绑定界面的验证码
    @Override
    public void smsbind(UserDTO userDTO) {
        String msg = "您的验证码为：";//短信模版
        //判断空值
        if (Objects.isNull(userDTO.getMobile())) {
            throw new RuntimeException("手机号不能为空！");
        }
        //生成手机验证码
        String randomString = StrUtils.getRandomString(6);
        //判断redis是否已经保存了验证码，没有就生成新的验证码发送
        Map<String, Object> entries = redisTemplate.opsForHash().entries("bind_" + userDTO.getMobile());
        if (entries.isEmpty()) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("smsCode", randomString);
            map1.put("smsCodeTime", new Date().getTime());
            //存redis
            redisTemplate.opsForHash().putAll("bind_" + userDTO.getMobile(), map1);
            //发送短信
            String result = SendMsgWebchineseUtil.doSendMsg(userDTO.getMobile(), msg + randomString);
        } else {
            //判断是否在60s内
            Long smsCodeTime = (Long) entries.get("smsCodeTime");
            if ((new Date().getTime() - smsCodeTime) <= 60000) {
                throw new RuntimeException("60s内不能重复发送！");
            }
            //获取旧的验证码
            String smsCode = (String) entries.get("smsCode");
            //发送旧的验证码
            String result = SendMsgWebchineseUtil.doSendMsg(userDTO.getMobile(), msg + smsCode);
            //更新redis过期时间为5分钟
            redisTemplate.expire("bind_" + userDTO.getMobile(), 5, TimeUnit.MINUTES);
        }

    }
}
